<template>
  <div id='Orders'>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row>
        <el-col :span="6">
          <!-- 搜索输入框区域 -->
          <el-input placeholder="请输入内容" v-model.trim="getOrderRequest.query" class="input-with-select" clearable
            @clear="clearIpt" width="50%" @keydown.enter.native="searchIpt" size="small">
            <el-button slot="append" icon="el-icon-search" @click="searchIpt"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 列表数据表格区域 -->
      <el-table :data="orderList.goods" border stripe>
        <el-table-column label="#" type="index">
        </el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="400">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格">
        </el-table-column>
        <el-table-column prop="order_pay" label="是否付款">
          <template slot-scope="scope">
            <el-tag size="small" v-if="scope.row.order_pay === '1'">已付款</el-tag>
            <el-tag size="small" type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货">
        </el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">{{ scope.row.create_time | delTime }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <el-tooltip effect="dark" content="修改订单地址" placement="top" :enterable="false">
              <el-button type="primary" size="mini" icon="el-icon-edit" @click="showEditOrderDialog"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="物流信息" placement="top" :enterable="false">
              <el-button plain type="success" size="mini" icon="el-icon-location-information"
                @click="showProgressDialog">
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="getOrderRequest.pagenum" :page-sizes="[1, 3, 5, 8]" :page-size="getOrderRequest.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="orderList.total">
      </el-pagination>
    </el-card>
    <!-- 修改订单地址的弹出框 -->
    <el-dialog title="修改地址" :visible.sync="editAddressDialog" width="30%" @close="clearEditAddressDialog">
      <el-form :model="editAddressForm" :rules="editAddressRules" ref="editAddressRef" label-width="90px">
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader :options="cityData" v-model="editAddressForm.address1"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="editAddressForm.address2" size="small"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="editAddressDialog = false">取 消</el-button>
        <!-- todo:修改订单地址的功能完善 -->
        <el-button type="primary" @click="subEditAddressForm">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流信息的弹出框 -->
    <el-dialog title="物流进度" :visible.sync="progressDialog" width="30%" @close="clearProgressDialog">
      <el-timeline>
        <el-timeline-item v-for="(activity, index) in goodsIfo" :key="index" :timestamp="activity.time">
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from '@/plugins/citydata.js'
export default {
  name: 'Orders',
  created() {
    this.getOrderList()
  },
  data() {
    return {
      goodsIfo: [],
      progressDialog: false,
      editAddressForm: {
        address1: [],
        address2: ''
      },
      // 订单数据信息
      orderList: {},
      // 获取订单数据的请求体
      getOrderRequest: {
        query: '',
        pagenum: 1,
        pagesize: 8
      },
      cityData,
      editAddressDialog: false,
      editAddressRules: {
        address1: [
          { required: true, message: '请输入省市区/县', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      // 订单物流的假数据(后端物流数据获取失败)
      fakeData: {
        'data': [
          {
            'time': '2018-05-10 09:39:00',
            'ftime': '2018-05-10 09:39:00',
            'context': '已签收,感谢使用顺丰,期待再次为您服务',
            'location': ''
          },
          {
            'time': '2018-05-10 08:23:00',
            'ftime': '2018-05-10 08:23:00',
            'context': '[北京市]北京海淀育新小区营业点派件员 顺丰速运 95338正在为您派件',
            'location': ''
          },
          {
            'time': '2018-05-10 07:32:00',
            'ftime': '2018-05-10 07:32:00',
            'context': '快件到达 [北京海淀育新小区营业点]',
            'location': ''
          },
          {
            'time': '2018-05-10 02:03:00',
            'ftime': '2018-05-10 02:03:00',
            'context': '快件在[北京顺义集散中心]已装车,准备发往 [北京海淀育新小区营业点]',
            'location': ''
          },
          {
            'time': '2018-05-09 23:05:00',
            'ftime': '2018-05-09 23:05:00',
            'context': '快件到达 [北京顺义集散中心]',
            'location': ''
          },
          {
            'time': '2018-05-09 21:21:00',
            'ftime': '2018-05-09 21:21:00',
            'context': '快件在[北京宝胜营业点]已装车,准备发往 [北京顺义集散中心]',
            'location': ''
          },
          {
            'time': '2018-05-09 13:07:00',
            'ftime': '2018-05-09 13:07:00',
            'context': '顺丰速运 已收取快件',
            'location': ''
          },
          {
            'time': '2018-05-09 12:25:03',
            'ftime': '2018-05-09 12:25:03',
            'context': '卖家发货',
            'location': ''
          },
          {
            'time': '2018-05-09 12:22:24',
            'ftime': '2018-05-09 12:22:24',
            'context': '您的订单将由HLA（北京海淀区清河中街店）门店安排发货。',
            'location': ''
          },
          {
            'time': '2018-05-08 21:36:04',
            'ftime': '2018-05-08 21:36:04',
            'context': '商品已经下单',
            'location': ''
          }
        ],
        'meta': { 'status': 200, 'message': '获取物流信息成功！' }
      }
    }
  },
  methods: {
    async getGoodsIfo() {
      const { data, meta } = await this.$http.get('/kuaidi/1106975712662')
      if (meta.status !== 200) return this.$message.error('物流信息获取失败')
      this.goodsIfo = data
      console.log(this.goodsIfo)
    },
    async getOrderList() {
      const { data, meta } = await this.$http.get('orders', { params: this.getOrderRequest })
      if (meta.status !== 200) return this.$message.error('订单列表数据获取失败')
      this.orderList = data
      console.log(this.orderList)
    },
    clearIpt() {
      this.getOrderRequest.query = ''
      this.getOrderList()
    },
    handleSizeChange(val) {
      this.getOrderRequest.pagesize = val
      this.getOrderRequest.pagenum = 1
      this.getOrderList()
    },
    handleCurrentChange(val) {
      this.getOrderRequest.pagenum = val
      this.getOrderList()
    },
    searchIpt() {
      if (this.getOrderRequest.query === '') return this.$message.error('请输入搜索内容')
      this.getOrderList()
    },
    showEditOrderDialog() {
      this.editAddressDialog = true
    },
    subEditAddressForm() {
      this.editAddressDialog = false
      console.log(this.editAddressForm)
    },
    clearEditAddressDialog() {
      this.$refs.editAddressRef.resetFields()
    },
    showProgressDialog() {
      this.getGoodsIfo()
      this.progressDialog = true
      // this.open1()
    },
    open1() {
      this.$Notification.warning({
        title: '注意',
        message: '开发过程中后端数据获取失败,该物流信息采用的是假数据'
      })
    },
    clearProgressDialog() {
      this.goodsIfo = []
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.input-with-select {
  background-color: #fff;
}

/deep/.el-input-group {
  margin-bottom: 15px;
}

/deep/.el-cascader {
  width: 100%;
}
</style>
